package com.bci.pwtz.common.util;

import org.apache.log4j.helpers.LogLog;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.web.context.ContextLoader;

public class SpringUtil implements ApplicationContextAware
{

    private static ApplicationContext applicationContext;

    @Override
    public void setApplicationContext(ApplicationContext context) throws BeansException
    {
        SpringUtil.applicationContext = context;
    }

    public static Object getBean(String name) throws BeansException
    {
        if (applicationContext == null)
            applicationContext = ContextLoader.getCurrentWebApplicationContext();
        if (applicationContext == null)
        {
            LogLog.error("applicationContext is null when get bean " + name);
        }
        else
            return applicationContext.getBean(name);
        return null;
    }
}